<style>
    textarea.tah {
        height: calc(1.5em + .75rem + 2px);
        min-height: calc(1.5em + .75rem + 2px);
    }
</style>

<div class="container-fluid">
    <div class="row">
        <div class="col-md-6">
            <div class="input-group">
                <div class="input-group-prepend">
                    <span class="input-group-text">生成</span>
                </div>
                <textarea id="txt" class="form-control tah" placeholder="内容：网址、电话、邮箱等"></textarea>
                <div class="input-group-prepend">
                    <button class="btn btn-warning nrBtnConfig">配置</button>
                </div>
            </div>
            <textarea class="form-control mt-3 min300 d-none nrTxtConfig"></textarea>
            <div id="divCodeResult" class="mt-3"></div>
        </div>
        <div class="col-md-6">
            <div class="input-group">
                <div class="input-group-prepend">
                    <span class="input-group-text">识别</span>
                </div>
                <div class="custom-file">
                    <input id="txtFile" type="file" class="custom-file-input" accept="image/*">
                    <label class="custom-file-label" id="pr">选择图片 或 拖拽图片文件 或 Ctrl + V 粘贴图片</label>
                </div>
            </div>
            <textarea class="form-control mt-3 nrResultCode d-none min300"></textarea>
        </div>
    </div>
</div>

@Html.Raw(Netnr.SharedApp.QuoteTo.Html("qrcode.js,jsqr.js"))

<script>
    var qrc = {
        defaultConfig: {
            errorCorrectionLevel: 'H',
            type: 'image/jpeg',
            quality: 0.3,
            width: 300,
            margin: 2,
            color: {
                dark: "#000000",
                light: "#FFFFFF"
            }
        },
        init: function () {
            $('#txt,.nrTxtConfig').on('input', function () {
                qrc.encode();
            });

            $('.nrBtnConfig').click(function () {
                if ($('.nrTxtConfig').hasClass('d-none')) {
                    $('.nrTxtConfig').removeClass('d-none')
                } else {
                    $('.nrTxtConfig').addClass('d-none')
                }
                if ($('.nrTxtConfig').val() == "") {
                    $('.nrTxtConfig').val(JSON.stringify(qrc.defaultConfig, null, 4));
                }
            });

            //接收
            ss.receiveFiles(function (files) {
                var file = files[0];
                qrc.decode(file);
            }, "#txtFile");
        },
        encode: function () {
            var txt = $('#txt').val();
            var options = qrc.defaultConfig;
            try {
                options = JSON.parse($('.nrTxtConfig').val());
            } catch (e) { }

            QRCode.toDataURL(txt, options, function (err, qrout) {
                if (err) {
                    console.log(err);
                    jz.msg("生成出错");
                } else {
                    var img = new Image();
                    img.src = qrout;
                    img.className = "border";
                    $('#divCodeResult').empty().append(img);
                }
            })
        },
        decode: function (file) {
            var fr = new FileReader();
            fr.readAsDataURL(file);
            fr.onloadend = function (e) {

                qrc.canvas = document.createElement("canvas");

                var img = new Image();
                img.src = e.target.result;
                img.onload = function () {
                    var ctx = qrc.canvas.getContext("2d");
                    qrc.canvas.width = img.width;
                    qrc.canvas.height = img.height;
                    ctx.drawImage(img, 0, 0, img.width, img.height);
                    var imageData = ctx.getImageData(0, 0, img.width, img.height);

                    var code = jsQR(imageData.data, imageData.width, imageData.height, {
                        inversionAttempts: "dontInvert",
                    });
                    if (code) {
                        console.log(code);
                        $('.nrResultCode').removeClass('d-none').val(code.data);
                    } else {
                        jz.msg("识别二维码失败")
                    }
                }
            }
        }
    }

    qrc.init();
</script>
